
import axios, { AxiosResponse } from 'axios'
import type { ApiResponse } from '~/types/api'

const api = axios.create({
    // baseURL: 'http://111.160.49.98:60',
    baseURL: 'http://127.0.0.1:8000',
    timeout: 5000
})

export const emailTask = async (formData: FormData): Promise<ApiResponse> => {
    try {
        const response: AxiosResponse<ApiResponse> = await api.post('/Species/user/validate', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })

        // 根据响应码判断是否成功
        if (response.data.code === 200) {
            return {
                success: true,
                msg: '邮箱验证码发送成功！',
                data: response.data.data,
                error: ''
            }
        } else {
            return {
                success: false,
                msg: response.data.msg || '邮箱验证码发送失败！',
                error: response.data.error || '未知错误'
            }
        }
    } catch (error: any) {
        // 处理请求失败的情况
        return {
            success: false,
            msg: error.response?.data?.msg || '邮箱验证码发送失败！',
            error: error.message || '网络错误'
        }
    }
}


export const signupTask = async (formData: FormData): Promise<ApiResponse> => {
    try {
        const response: AxiosResponse<ApiResponse> = await api.post('/Species/user/regiter', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })

        // 根据响应码判断是否成功
        if (response.data.code === 200) {
            return {
                success: true,
                msg: '注册成功！',
                data: response.data.data,
                error: ''
            }
        } else {
            return {
                success: false,
                msg: response.data.msg || '注册失败！',
                error: response.data.error || '未知错误'
            }
        }
    } catch (error: any) {
        // 处理请求失败的情况
        return {
            success: false,
            msg: error.response?.data?.msg || '注册错误！',
            error: error.message || '网络错误'
        }
    }
}

export const PasswordTask = async (formData: FormData): Promise<ApiResponse> => {
    try {
        const response: AxiosResponse<ApiResponse> = await api.put('/Species/user/updatePwd', formData, {
            headers: {
                'Content-Type': 'application/json'
            }
        });

        // 根据响应码判断是否成功
        if (response.data.code === 200) {
            return {
                success: true,
                msg: '密码重置成功！',
                data: response.data.data,
                error: ''
            }
        } else {
            return {
                success: false,
                msg: response.data.msg || '密码重置失败！',
                error: response.data.error || '未知错误'
            }
        }
    } catch (error: any) {
        // 处理请求失败的情况
        return {
            success: false,
            msg: error.response?.data?.msg || '密码重置错误！',
            error: error.message || '网络错误'
        }
    }
}